<?php
/**
 * Subject manager
 */
define ( 'PRIORITY_HIGHEST', 1 );
define ( 'PRIORITY_HIGH', 2 );
define ( 'PRIORITY_MEDIUM', 3 );
define ( 'PRIORITY_LOW', 4 );

define ( 'STATE_OPEN', 1 );
define ( 'STATE_ONGOING', 2 );
define ( 'STATE_FINISH', 3 );
define ( 'STATE_CLOSE', 4 );

elgg_register_event_handler ( 'init', 'system', 'submanage_init' );
function submanage_init() {
	elgg_register_library ( 'elgg:subjectmanagement', elgg_get_plugins_path () . 'subjectmanage/lib/subjectmanagement.php' );
	
	elgg_register_plugin_hook_handler ( 'register', 'menu:owner_block', 'subjectmanage_owner_block_menu' );
	elgg_register_page_handler ( 'submana', 'submanage_page_handler' );
	
	$action_base = elgg_get_plugins_path () . "/subjectmanage/actions";
	elgg_register_action ( 'addprojects', "$action_base/addprojects.php" );
	elgg_register_action ( 'registerproject', "$action_base/registerproject.php" );
	elgg_register_action ( 'editprojectlist_deadline', "$action_base/editprojectlist_deadline.php" );
	elgg_register_action ( 'editproject', "$action_base/editproject.php" );
	elgg_register_action ( 'assigntask', "$action_base/assigntask.php" );
	elgg_register_action ( 'submitasg', "$action_base/submitasg.php" );
	elgg_register_action ( 'reuse_projects', "$action_base/reuse_projects.php" );
	elgg_register_action ( 'registeroutlist', "$action_base/registeroutlist.php" );
	
	elgg_extend_view ( 'css/elgg', 'css/main' );
	
	// register object for searching
	elgg_register_entity_type ( 'object', 'project' );
	
	$js_url = 'mod/subjectmanage/vendors/raty/jquery.raty.js';
	elgg_register_js ( 'jquery-raty', $js_url );
}

// Register menu items for group profile
function subjectmanage_owner_block_menu($hook, $type, $return, $params) {
	$user = elgg_get_logged_in_user_entity ();
	if (elgg_instanceof ( $params ['entity'], 'group' )) {
		$group = $params ['entity'];
		if ($group->grouptype == SUBJECT_GROUP) {
			if ($group->canEdit ()) {
				$url = "submana/addlistpro/{$params['entity']->guid}";
				$item = new ElggMenuItem ( 'addlistproject', elgg_echo ( 'submana:addlistpro' ), $url );
				$item->setPriority ( 90 );
				$return [] = $item;
			}
			$url = "submana/listpro/{$params['entity']->guid}";
			$item = new ElggMenuItem ( 'listproject', elgg_echo ( 'submana:listpro' ), $url );
			$item->setPriority ( 91 );
			$return [] = $item;
		} else if ($group->grouptype == PROJECT_GROUP) {
			
			// $assign_all_url = "submana/assignall/$group->register_project";
			// $assig_all = elgg_view ( 'output/url', array (
			// 'href' => $assign_all_url,
			// 'text' => elgg_echo ( 'submana:assignall' ),
			// 'is_action' => true,
			// 'is_trusted' => true
			// ) ); //. "&nbsp &nbsp";
			
			$url = "submana/project/{$group->getGUID()}/assignall/$group->register_project";
			
			$item = new ElggMenuItem ( 'assignall', elgg_echo ( "Bài tập nhóm" ), $url );
			$item->setPriority ( 91 );
			$return [] = $item;
		}
	} else if (elgg_instanceof ( $params ['entity'], 'user' )) {
		$current_user = $params ['entity'];
		if ($current_user->usertype == "Teacher") {
			$url = "submana/subjects/{$current_user->username}";
			$item = new ElggMenuItem ( 'submana', elgg_echo ( 'submana:profile:submana' ), $url );
			$return [] = $item;
			
			$url = "groups/all?filter=review_teacher_make";
			$item = new ElggMenuItem ( 'review_teacher_make', 'Các đánh giá của tôi', $url );
			$return [] = $item;
			
			$url = "submana/subjects/{$current_user->username}";
			$item = new ElggMenuItem ( 'submana', elgg_echo ( 'submana:profile:submana' ), $url );
			$return [] = $item;
			
			$url = "groups/all?filter=review_teacher_make";
			$item = new ElggMenuItem ( 'review_teacher_make', 'Các đánh giá của tôi', $url );
			$return [] = $item;
		} else if ($current_user->usertype == "Student") {
			$url = "submana/joinedprojects/{$current_user->username}";
			$item = new ElggMenuItem ( 'projects', elgg_echo ( 'submana:profile:joinedprojects' ), $url );
			
			$return [] = $item;
		}
	}
	
	return $return;
}

/*
 * main handle page
 */
function submanage_page_handler($page) {
	$pages_path = elgg_get_plugins_path () . 'subjectmanage/pages';
	$group_guid = $page [1];
	$vars ['group_guid'] = $group_guid;
	
	switch ($page [0]) {
		case 'addlistpro' :
			elgg_set_page_owner_guid ( $group_guid );
			$vars ['reused'] = $page [2];
			$vars ['semester_reused'] = $page [3];
			$vars ['academic_year_reused'] = $page [4];
			$vars ['extra'] = $page [5];
			include "$pages_path/addprojects.php";
			break;
		case 'listpro' :
			
			elgg_load_library ( 'elgg:subjectmanagement' );
			elgg_set_page_owner_guid ( $group_guid );
			if (isset ( $page [2] ) && $page [2] == "close") {
				$vars ['class_state_show'] = STATE_CLOSE;
			}
			include "$pages_path/listpro.php";
			break;
		case 'project' :
			return handle_project_page ( $page, $pages_path );
		case 'editprojectlistdeadline' :
			$vars ['projectlist_guid'] = $page [1];
			$vars ['projectlist_name'] = $page [2];
			$vars ['semester'] = $page [3];
			$vars ['academic_year'] = $page [4];
			include "$pages_path/editprojectlist_deadline.php";
			break;
		case "joinedprojects" :
			$vars ['list_user'] = true;
			include "$pages_path/listpro.php";
			break;
		case "subjects" :
			$vars ['teacher'] = $page [1];
			include "$pages_path/subjects.php";
			break;
		case 'assign' :
			handle_assignment_page ( $page, $pages_path );
			break;
		case 'deleteprojectlist' :
			$vars ['group_project'] = $page [2];
			$vars ['semester'] = $page [3];
			$vars ['academic_year'] = $page [4];
			
			$action_path = elgg_get_plugins_path () . 'subjectmanage/actions';
			include "$action_path/deleteprojectlist.php";
			break;
		case 'reportproject' :
			elgg_set_page_owner_guid ( $group_guid );
			$vars ['group_project'] = $page [2];
			$vars ['semester'] = $page [3];
			$vars ['academic_year'] = $page [4];
			include "$pages_path/reportproject.php";
			break;
		case 'registeroutlist' :
			elgg_set_page_owner_guid ( $group_guid );
			$vars ['group_project'] = $page [2];
			$vars ['semester'] = $page [3];
			$vars ['academic_year'] = $page [4];
			$vars ['projectlist_guid'] = $page [5];
			include "$pages_path/registeroutlist.php";
			break;
		case "closeclass" :
			$action_path = elgg_get_plugins_path () . 'subjectmanage/actions';
			action_gatekeeper ( "closeproject" );
			$vars ['group_project'] = $page [2];
			$vars ['semester'] = $page [3];
			$vars ['academic_year'] = $page [4];
			include "$action_path/closeclass.php";
			break;
		default :
			return false;
	}
	
	return true;
}
/*
 * Handel project page "register"		student register a project in list project "edit" 			teacher edit a project "del"			teacher delete a project "approve"		teacher approve a registration "reject"			teacher reject a registration "assign" 		teacher assign an task "submitasg"		student submit an assignment "assignall" 		view all assignment of a project
 */
function handle_project_page($page, $pages_path) {
	$vars ['project_guid'] = $page [3];
	$vars ['group_guid'] = $page [1];
	elgg_set_page_owner_guid ( $page [1] );
	switch ($page [2]) {
		case "register" :
			include "$pages_path/registerproject.php";
			break;
		case "edit" :
			include "$pages_path/editproject.php";
			break;
		case "del" :
			$action_path = elgg_get_plugins_path () . 'subjectmanage/actions';
			include "$action_path/delproject.php";
			break;
		case "approve" :
			$action_path = elgg_get_plugins_path () . 'subjectmanage/actions';
			$vars ['approve'] = true;
			include "$action_path/approval.php";
			break;
		case "reject" :
			$action_path = elgg_get_plugins_path () . 'subjectmanage/actions';
			$vars ['approve'] = false;
			include "$action_path/approval.php";
			break;
		case "assign" :
			include "$pages_path/assigntask.php";
			break;
		case "submitasg" :
			$vars ['assign_guid'] = $page [3];
			include "$pages_path/submitasg.php";
			break;
		case "assignall" :
			include "$pages_path/assignall.php";
			break;
		case "outregister" :
			$action_path = elgg_get_plugins_path () . 'subjectmanage/actions';
			include "$action_path/outregister.php";
			break;
		default :
			return false;
	}
	
	return true;
}

/*
 * Handel assignment page "del"		Delete an assignment "edit" 		Edit an assignment
 */
function handle_assignment_page($page, $pages_path) {
	switch ($page [1]) {
		case "edit" :
			$vars ['assignment_guid'] = $page [2];
			$vars ['group_guid'] = $page [3];
			include "$pages_path/assigntask.php";
			break;
		case "del" :
			$vars ['assignment_guid'] = $page [2];
			$action_path = elgg_get_plugins_path () . 'subjectmanage/actions';
			include "$action_path/delassignment.php";
			break;
		default :
			return false;
	}
}